/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.comboBox;

import java.io.File;
import java.io.FileFilter;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;

import org.jxSwing.component.comboBox.cellRenderer.LabelListRenderer;
import org.jxUtils.check.ParameterCheck;
import org.jxUtils.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.comboBox
 * @date 14/08/2014 - 21:14:22
 */
public class FileComboBox extends ComboBox<File> {
	
	private static final long serialVersionUID = -7382165852818670231L;
	
	/**
	 * 
	 */
	public FileComboBox() {
	
		this(File.listRoots());
	}
	
	/**
	 * @param model
	 */
	public FileComboBox(final ComboBoxModel<File> model) {
	
		super(model);
		
		initGUI();
	}
	
	/**
	 * @param directory
	 * @param showHideFiles
	 * @param showOnlyDirectories
	 */
	public FileComboBox(final File directory, final boolean showHideFiles, final boolean showOnlyDirectories) {
	
		ParameterCheck.isNull(directory, "directory");
		
		//
		if( !directory.exists()) {
			
			throw new IllegalArgumentException(I18N.file().directoryNotExists(directory.getAbsolutePath()));
		}
		
		//
		if( !directory.isDirectory()) {
			
			throw new IllegalArgumentException(I18N.file().pathIsNotDirectory(directory.getAbsolutePath()));
		}
		
		//
		final File[] files = directory.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(final File pathName) {
			
				return pathName.isHidden() ? showHideFiles : !pathName.isDirectory() ? !showOnlyDirectories : true;
			}
		});
		
		//
		if(files != null) {
			
			setItems(files);
		}
	}
	
	/**
	 * @param files
	 */
	public FileComboBox(final File[] files) {
	
		super(files);
		
		initGUI();
	}
	
	/**
	 * @param files
	 */
	public FileComboBox(final List<File> files) {
	
		super(files);
		
		initGUI();
	}
	
	/**
	 * @param files
	 */
	public FileComboBox(final Vector<File> files) {
	
		super(files);
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setRenderer(new LabelListRenderer());
	}
	
	/**
	 * @param fileName
	 * @return index
	 */
	public int getIndexByName(final String fileName) {
	
		//
		for(int index = 0; index < getItemCount(); index++) {
			
			//
			if(getItemAt(index).getName().equals(fileName)) {
				
				return index;
			}
		}
		
		return -1;
	}
	
	/**
	 * @param fileName
	 */
	public void setSelected(final String fileName) {
	
		setSelectedIndex(getIndexByName(fileName));
	}
}
